package com.example.radiationarea;

import com.example.radiationarea.block.ModBlocks;
import com.example.radiationarea.compat.BiomesOPlentyCompat;
import com.example.radiationarea.compat.TerraForgedCompat;
import com.example.radiationarea.compat.LostCitiesCompat;
import com.example.radiationarea.config.ModConfigs;
import com.example.radiationarea.effect.ModEffects;
import com.example.radiationarea.item.ModItems;
import com.example.radiationarea.world.biome.ModBiomes;
import com.example.radiationarea.world.biome.RadiationRegion;
import com.example.radiationarea.world.biome.RadiationSurfaceRules;
import com.mojang.logging.LogUtils;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.slf4j.Logger;
import terrablender.api.Regions;
import terrablender.api.SurfaceRuleManager;

@Mod(RadiationAreaMod.MOD_ID)
public class RadiationAreaMod {
    public static final String MOD_ID = "radiation_area";
    public static final Logger LOGGER = LogUtils.getLogger();
    
    public static final CreativeModeTab RADIATION_TAB = new CreativeModeTab("radiation_area") {
        @Override
        public ItemStack makeIcon() {
            // 使用模组的logo作为创造物品栏图标
            return new ItemStack(ModItems.RADIATION_ICON.get());
        }
    };

    public RadiationAreaMod() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

        // Register the common setup method for modloading
        modEventBus.addListener(this::commonSetup);

        // Register the deferred registries
        ModBlocks.register(modEventBus);
        ModItems.register(modEventBus);
        ModEffects.register(modEventBus);
        ModBiomes.BIOMES.register(modEventBus);

        // Register ourselves for server and other game events we are interested in
        MinecraftForge.EVENT_BUS.register(this);
        
        // Register configs
        ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ModConfigs.SERVER_SPEC);
        
        LOGGER.info("Radiation Area mod constructor completed");
        LOGGER.debug("Registered blocks, items, effects, and biomes with mod event bus");
    }

    private void commonSetup(final FMLCommonSetupEvent event) {
        LOGGER.info("Starting Radiation Area mod initialization");
        LOGGER.info("Mod ID: {}", MOD_ID);
        
        // Verify TerraBlender presence
        if (ModList.get().isLoaded("terrablender")) {
            LOGGER.info("TerraBlender detected, registering radiation region");
            event.enqueueWork(() -> {
                try {
                    // Register our radiation region
                    Regions.register(new RadiationRegion());
                    LOGGER.info("Registered RadiationRegion with TerraBlender");
                    
                    // Register surface rules
                    SurfaceRuleManager.addSurfaceRules(
                        SurfaceRuleManager.RuleCategory.OVERWORLD, 
                        MOD_ID, 
                        RadiationSurfaceRules.makeRadiationSurfaceRules()
                    );
                    LOGGER.info("Registered surface rules for RadiationRegion");
                } catch (Exception e) {
                    LOGGER.error("Failed to register radiation region with TerraBlender", e);
                }
            });
        } else {
            LOGGER.info("TerraBlender not detected, skipping radiation region registration");
        }
        
        // Check for Biomes O' Plenty
        if (BiomesOPlentyCompat.isLoaded()) {
            LOGGER.info("Biomes O' Plenty detected, enabling compatibility features");
            event.enqueueWork(() -> {
                try {
                    BiomesOPlentyCompat.setupCompat();
                } catch (Exception e) {
                    LOGGER.error("Failed to set up Biomes O' Plenty compatibility", e);
                }
            });
        } else {
            LOGGER.info("Biomes O' Plenty not detected, skipping compatibility features");
        }
        
        // Check for TerraForged
        if (TerraForgedCompat.isLoaded()) {
            LOGGER.info("TerraForged detected, enabling compatibility features");
            event.enqueueWork(() -> {
                try {
                    TerraForgedCompat.setupCompat();
                } catch (Exception e) {
                    LOGGER.error("Failed to set up TerraForged compatibility", e);
                }
            });
        } else {
            LOGGER.info("TerraForged not detected, skipping compatibility features");
        }
        
        // Check for Lost Cities
        if (LostCitiesCompat.isLoaded()) {
            LOGGER.info("Lost Cities detected, enabling compatibility features");
            event.enqueueWork(() -> {
                try {
                    LostCitiesCompat.setupCompat();
                } catch (Exception e) {
                    LOGGER.error("Failed to set up Lost Cities compatibility", e);
                }
            });
        } else {
            LOGGER.info("Lost Cities not detected, skipping compatibility features");
        }
        
        LOGGER.info("Radiation Area mod initialization completed");
    }
}